package com.example.aidocengine.controller;

import com.example.aidocengine.common.result.ApiResponse;
import com.example.aidocengine.pojo.dto.response.DeviceImportResponse;
import com.example.aidocengine.pojo.entity.CommandingUnit;
import com.example.aidocengine.pojo.entity.DeviceInfo;
import com.example.aidocengine.service.DeviceImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/device")
public class DeviceImportController {

    @Autowired
    private DeviceImportService deviceImportService;

    @PostMapping("/import")
    public ApiResponse<DeviceImportResponse> importDeviceTemplate(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "doClear", required = false) Boolean doClear) {
        log.info("是否清空：{}", doClear);
        
        log.info("收到设备模板导入请求，文件名：{}", file.getOriginalFilename());
        
        DeviceImportResponse response = deviceImportService.importDeviceTemplate(file, doClear, description);
        return ApiResponse.success("导入成功", response);
    }

    @GetMapping("/import/{importId}")
    public ApiResponse<DeviceImportResponse> getImportRecord(
            @PathVariable Long importId) {
        
        DeviceImportResponse response = deviceImportService.getImportRecord(importId);
        return ApiResponse.success(response);
    }

    @GetMapping("/list")
    public ApiResponse<List<DeviceInfo>> getDeviceList() {
        
        log.info("收到获取设备列表请求");
        
        List<DeviceInfo> devices = deviceImportService.getDeviceList();
        return ApiResponse.success("查询成功", devices);
    }
    
    @GetMapping("/commanding-units")
    public ApiResponse<List<CommandingUnit>> getCommandingUnitList() {
        
        log.info("收到获取受令单位列表请求");
        
        List<CommandingUnit> commandingUnits = deviceImportService.getCommandingUnitList();
        return ApiResponse.success("查询成功", commandingUnits);
    }
}